<?php

/**
 * 配件管理
 */

namespace app\admin\controller;

use think\Request;
use app\admin\service\Attachment as AttachmentService;
use app\admin\validate\Attachment as AttachmentValidate;

class Attachment extends BaseController
{
    /**
     * 获取配件列表
     * @param Request $request
     * @param AttachmentService $AttachmentService
     * @param AttachmentValidate $validate
     * @return \think\response\Json
     */
    public function getList(Request $request, AttachmentService $AttachmentService, AttachmentValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['cname'] =  $request->param('cname', null);
        $param['start_date'] =  $request->param('start_date', null);
        $param['end_date'] =  $request->param('end_date', null);
        $param['sku'] =  $request->param('sku', null);
        $param['code'] =  $request->param('code', null);
        $param['brand'] =  $request->param('brand', null);
        $param['abbr'] = $request->param('abbr', null); // 缩写
        $param['category'] = $request->param('category', null); // 分类
        $param['is_merge_product'] = $request->param('is_merge_product', null); // 是否关联聚合商品
        $param['is_have_inventory'] = $request->param('is_have_inventory', null); // 是否有库存

        $intPage =  $request->param('pageIndex', 1);
        $intPageSize =  $request->param('pageSize', 50);
        $arrData = $AttachmentService->getList($param, (int)$intPage, (int)$intPageSize);
        return json($arrData);
    }

    /**
     * 配件添加和修改
     * @param Request $request
     * @param AttachmentService $AttachmentService
     * @param AttachmentValidate $validate
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(Request $request, AttachmentService $AttachmentService, AttachmentValidate $validate)
    {
        $param = $request->param();
        if (!$validate->scene('create')->check($param)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;

        $arrData = $AttachmentService->create($param);
        return json($arrData);
    }


    /**
     * 配件修改
     * @param Request $request
     * @param AttachmentService $AttachmentService
     * @param AttachmentValidate $validate
     * @return \think\response\Json|void
     */
    public function update(Request $request, AttachmentService $AttachmentService, AttachmentValidate $validate)
    {
        $param = $request->param();
        if (!$validate->scene('update')->check($param)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['operator_id'] = $request->operator_id;
        $condation['id'] = $param['id'];
        unset($param['id']);
        $arrData = $AttachmentService->update($condation, $param);
        return json($arrData);
    }
    /**
     * 配件详情
     * @param Request $request 配件ID
     * @return \think\response\Json 返回数据
     * @author huangfeng
     * @date 2024-6-28
     */
    public function getInfo(Request $request, AttachmentService $AttachmentService, AttachmentValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('show')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['id'] = $request->param('id', null);
        $arrData = $AttachmentService->getInfo($param);
        return json($arrData);
    }


    /**
     * 删除单条或多条数据
     * @param Request $request
     * @param AttachmentValidate $validate
     * @param AttachmentService $AttachmentService
     * @return \think\response\Json
     */
    public function delete(Request $request, AttachmentValidate $validate, AttachmentService $AttachmentService)
    {
        $checkData = $request->param();
        if (!$validate->scene('delete')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['ids'] = $request->param('ids', null);
        $arrData = $AttachmentService->delete($param);
        return json($arrData);
    }
}
